package com.woniuxy.enjoylife.controller;

import com.woniuxy.enjoylife.entity.dto.SuggestDto;
import com.woniuxy.enjoylife.service.SuggestService;
import com.woniuxy.enjoylife.utils.PageResult;
import com.woniuxy.enjoylife.utils.ResponseResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/suggest")
public class SuggestController {

    @Resource
    private SuggestService suggestService;
    @Resource
    private HttpServletRequest request;

    /**
     * 全查询投诉，包括用户，小区信息（分页）
     *
     * @param current,size
     * @return
     */
    @RequestMapping("/findAll")
    public ResponseResult<PageResult> findAll(String content,int current, int size) {
        //根据登录获取
        int reuid = (int) request.getSession().getAttribute("id");
        ResponseResult responseResult = new ResponseResult();
        PageResult<List<SuggestDto>> pageResult;
        //如果有搜索，调用模糊搜索方法
        if(content != null && content != ""){
            pageResult = suggestService.findLike(reuid, content, current, size);
        }else {
            pageResult = suggestService.findAll(reuid,current, size);
        }
        if (pageResult == null) {
            responseResult.error();
        } else {
            responseResult.success();
            responseResult.setData(pageResult);
        }
        return responseResult;
    }

    /**
     * 根据uid进行回复，并添加回复人id
     *
     * @param id
     * @param reply
     * @return
     */
    @RequestMapping("/reply")
    public ResponseResult reply(int id, String reply) {
        ResponseResult responseResult = new ResponseResult();
        if (suggestService.reply(id, reply)) {
            responseResult.success();
        } else {
            responseResult.error();
        }
        System.out.println(responseResult);
        return responseResult;
    }

    /**
     * 根据内容模糊查询并分页
     *
     * @param content
     * @param current
     * @param size
     * @return
     */
//    @RequestMapping("/like")
//    public ResponseResult<PageResult> findLike(String content, int current, int size) {
//        //假数据
//        int reuid = (int) request.getSession().getAttribute("id");
//        ResponseResult responseResult = new ResponseResult();
//        PageResult<List<SuggestDto>> pageResult = suggestService.findLike(reuid,content,current,size);
//        if (pageResult == null) {
//            responseResult.error();
//        } else {
//            responseResult.success();
//            responseResult.setData(pageResult);
//        }
//        return responseResult;
//    }

    /**
     * 根据投诉id查询uid,cid,再根据uid,cid查询用户，小区信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/findSuggestDtoById")
    public ResponseResult findSuggestDtoById(int id) {
        SuggestDto suggestDto = suggestService.findSuggestDtoById(id);
        ResponseResult responseResult = new ResponseResult();
        if (suggestDto == null) {
            responseResult.error();
        } else {
            responseResult.success();
            responseResult.setData(suggestDto);
        }
        return responseResult;
    }

    /**
     * 根据投诉id删除（将status改为1）
     */
    @RequestMapping("/del")
    public ResponseResult del(int id) {
        ResponseResult responseResult = new ResponseResult();
        if (suggestService.del(id)) {
            responseResult.success();
            responseResult.setMessage("删除成功！");
        } else {
            responseResult.error();
            responseResult.setMessage("删除失败！");
        }
        return responseResult;
    }

    //***************************用户投诉*********

    /**
     * 用户分页查询投诉与建议
     * @param current
     * @param size
     * @return
     */
    @RequestMapping("/findUserSuggest")
    @ResponseBody
    public ResponseResult<PageResult> findUserSuggest(int current, int size) {
        System.out.println("findUserSuggest");
        //uid为用户id
        int uid = (int) request.getSession().getAttribute("id");
        ResponseResult responseResult = new ResponseResult();
        PageResult<List<SuggestDto>> pageResult = suggestService.findUserSuggest(uid,current,size);
        if (pageResult == null) {
            responseResult.error();
        } else {
            responseResult.success();
            responseResult.setData(pageResult);
        }
        return responseResult;
    }

    /**
     * 用户投诉
     * @param id  小区id
     * @param content  投诉内容
     * @return
     */
    @RequestMapping("/insertSuggest")
    @ResponseBody
    public ResponseResult insertSuggest(String id,String content){
        //uid 为用户id
        int uid = (int) request.getSession().getAttribute("id");
        System.out.println(id+":"+content);
        ResponseResult responseResult = new ResponseResult();

        if(suggestService.insertSuggest(uid,Integer.parseInt(id),content)){
            responseResult.success();
            responseResult.setMessage("投诉成功");
        }else {
            responseResult.error();
            responseResult.setMessage("投诉失败，请稍后再试");
        }
        return responseResult;
    }
}
